﻿using MyWoW.Classes;
using MyWoW.FiniteStateMachine;
using SBSimpleLib.Utils;
using ShadowBot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SBSimpleLib.Utils
{
    public class Utilities
    {
        private static String TAG = "UTL";

        public static void dumpStates(String requester)
        {
            Logger.debug(TAG, "Dumping states");

            int index = 0;
            while (index < API.Bot.Overrides.FiniteStateMachine.Engine.States.Count)
            {
                State currentState = API.Bot.Overrides.FiniteStateMachine.Engine.States[index];
                Logger.debug(requester, currentState.DisplayName + ", priority: " + currentState.Priority);
                index++;
            }
        }

        public static void dumpCharStatus(String requester)
        {
            Logger.debug(requester, "Account: " + MyWoW.ObjectManager.ObjectManager.Me.Account);
            Logger.debug(requester, "Realm: " + MyWoW.ObjectManager.ObjectManager.Me.Realm);
            Logger.debug(requester, "Name: " + MyWoW.ObjectManager.ObjectManager.Me.Name);
            Logger.debug(requester, "Level: " + MyWoW.ObjectManager.ObjectManager.Me.Level);
            Logger.debug(requester, "Class: " + MyWoW.ObjectManager.ObjectManager.Me.Class);
            Logger.debug(requester, "Race: " + MyWoW.ObjectManager.ObjectManager.Me.Race);
            Logger.debug(requester, "MinimapZone/SubZone: " + MyWoW.ObjectManager.ObjectManager.Me.GetMinimapZoneText + "/" + MyWoW.ObjectManager.ObjectManager.Me.GetSubZoneText);

            Logger.debug(requester, "Destination: " + ShadowBot.API.Bot.Navigator.GetDestination + ", isRunning: " + ShadowBot.API.Bot.Navigator.IsRunning);
            Logger.debug(requester, "CurrentSubPath: " + ShadowBot.API.Bot.Path.CurrentSubPath.UniqueName + ", LastProfilePath: " + API.Bot.Path.LastProfilePath);
            Logger.debug(requester, "MailBox: " + ShadowBot.API.Bot.Path.CurrentSubPath.Mailbox);

        }

        public static String formatPosition(Position pos, bool includeZ = false, String format = "{0:00.0}")
        {
            String x = formatDouble(pos.X, format);
            String y = formatDouble(pos.Y, format);
            if (!includeZ)
            {
                return "[" + x + "," + y + "]";
            }
            else
            {
                String z = formatDouble(pos.Z, format);
                return "[" + x + "," + y + "," + z + "]";
            }
        }

        public static String formatDouble(double value, String format = "{0:00.0}")
        {
            String ret = String.Format(format, value);
            return ret;
        }

        public static Position getCloserPosition(List<Position> positions, Position position)
        {
            Position ret = null;
            double closerDistance = 9.9E99;
            foreach (Position currentPosition in positions)
            {
                double tmpDistance = position.Distance3D(currentPosition);
                if (tmpDistance < closerDistance)
                {
                    ret = currentPosition;
                    closerDistance = tmpDistance;
                }
            }
            return ret;
        }

        public static double getCurrentMailBoxDistance()
        {
            Double mailBoxDistance = 9.9E99;
            Position mailBox = ShadowBot.API.Bot.Path.CurrentSubPath.Mailbox;
            Position playerPos = MyWoW.ObjectManager.ObjectManager.Me.Position;
            String currentSubPath = ShadowBot.API.Bot.Path.CurrentSubPath.UniqueName;
            if (currentSubPath != null && !currentSubPath.Equals(""))
            {
                if (mailBox != null && mailBox.X != 0 && mailBox.Y != 0 && mailBox.Z != 0)
                {
                    mailBoxDistance = playerPos.Distance3D(mailBox);
                }
                else
                {
                    throw new Exception("You must define MailBox in your current SubPath.");
                }
            }
            else
            {
                throw new Exception("No current Subpath");
            }

            return mailBoxDistance;
        }
    }
}
